home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSAEClose.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  4.1 KB  |  170 lines  |  [TEXT/CWIE]

  1. // MSAEClose.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. /*
  8.     Changes for 4.0
  9.  
  10.     28-Feb-96 : GS : Added differentiation between windows and documents.
  11. */
  12.  
  13. #include "MSAEClose.h"
  14.  
  15. #include "MSAEUtils.h"
  16. #include "MSWindow.h"        // for DPtrFromWindowPtr()
  17. #include "MSFile.h"            // for DoClose()
  18.  
  19.  
  20. #pragma segment AppleEvent
  21.  
  22.  
  23. pascal OSErr DoCloseWindow(const AppleEvent        *theAppleEvent,
  24.                                 AppleEvent        *reply,
  25.                                 long            handlerRefCon)
  26. {
  27. #ifdef __MWERKS__
  28.     #pragma unused (reply, handlerRefCon)
  29. #endif
  30.  
  31.     AEDesc            directObj = {typeNull, NULL};
  32.     DescType         saveOpt;
  33.     Size              actualSize;
  34.     DescType          returnedType;
  35.     WindowToken        aWindowToken;
  36.     OSErr             err;
  37.     
  38.         // pick up the direct object, which is the object (window) to close or a list of windows
  39.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  40.                 
  41.         // pick up optional save param, if any
  42.     saveOpt = kAEAsk;     // the default
  43.     err = AEGetParamPtr(theAppleEvent, keyAESaveOptions, typeEnumerated, 
  44.                             &returnedType, (Ptr)&saveOpt, sizeof(saveOpt), &actualSize);
  45.                                                      
  46.     err = GotRequiredParams(theAppleEvent);
  47.     if (noErr != err) goto done;
  48.     
  49.     if (typeNull != directObj.descriptorType)
  50.         err = CloseDesc(&directObj, saveOpt);
  51.     else if ( FrontWindow() )                    // Default to front window if
  52.     {                                            // no direct object.
  53.         aWindowToken.tokenWindow = FrontWindow();
  54.         err = CloseWindowToken(&aWindowToken, saveOpt);
  55.     }
  56.     else
  57.         err = errAENoSuchObject;
  58.  
  59. done:
  60.     if (directObj.dataHandle) 
  61.         AEDisposeDesc(&directObj);
  62.             
  63.     return(err);
  64. }    // DoCloseWindow
  65.  
  66.  
  67. OSErr    CloseWindowToken(WindowToken* theToken, DescType saveOpt)
  68. {
  69.     WindowPtr        aWindow;
  70.     DPtr            docPtr;
  71.     OSErr            err;
  72.     
  73.     aWindow = theToken->tokenWindow;
  74.     docPtr = DPtrFromWindowPtr( theToken->tokenWindow );
  75.     
  76.     if ( ! aWindow )
  77.         return errAENoSuchObject;
  78.  
  79.         // Should do this in prefs
  80.     err = AESetInteractionAllowed( kAEInteractWithAll );
  81.     if ( noErr != err ) goto done;
  82.     
  83.     if ( docPtr )    // May be other kind of window
  84.     {
  85.             //    We do some of the close checks here to avoid
  86.             //    calling AEInteractWithUser
  87.         if ( docPtr->dirty || ( docPtr->everSaved == false ) ) 
  88.             if ( saveOpt != kAENo )    // Don't flip layers if force no ask
  89.                 err = AEInteractWithUser( kAEDefaultTimeout, NULL, NULL );
  90.     }
  91.  
  92.     if ( noErr != err ) goto done;
  93.         
  94.     err = DoClose( aWindow, true, saveOpt );
  95.     
  96.     if ( userCanceledErr == err )    // Don't put up an error if only cancelled
  97.         err = noErr;
  98.  
  99. done:        
  100.     return err;
  101. }
  102.  
  103.  
  104. OSErr    CloseWindowDesc(AEDesc* windowDesc, DescType saveOpt)
  105. {
  106.     WindowToken        aWindowToken;
  107.     Size            actualSize;
  108.     OSErr            err;
  109.  
  110.     if (typeMyWndw != windowDesc->descriptorType)
  111.         return errAETypeError ;
  112.         
  113.     GetRawDataFromDescriptor(windowDesc, (Ptr)&aWindowToken, sizeof(aWindowToken), &actualSize);
  114.  
  115.     err = CloseWindowToken(&aWindowToken, saveOpt);
  116.     
  117.     return(err);
  118. }
  119.  
  120.  
  121. OSErr    CloseDesc(AEDesc* aDesc, DescType saveOpt)
  122. {
  123.     AEDesc        closeDesc = {typeNull, NULL},
  124.                 windowDesc = {typeNull, NULL},
  125.                 itemDesc = {typeNull, NULL};
  126.     long        itemCount,
  127.                 index;
  128.     DescType    theAEKeyword;
  129.     OSErr        err;
  130.     
  131.     if ( typeObjectSpecifier == aDesc->descriptorType )
  132.         err = AEResolve(aDesc, kAEIDoMinimum, &closeDesc);
  133.     else if ( typeNull != aDesc->descriptorType )
  134.         err = AEDuplicateDesc(aDesc, &closeDesc);
  135.         
  136.     if (noErr != err) goto done;
  137.     
  138.     switch (closeDesc.descriptorType)
  139.     {
  140.         case typeAEList:
  141.             err = AECountItems(&closeDesc, &itemCount);
  142.             if (noErr != err) goto done;
  143.             
  144.             for (index = 1; index <= itemCount; index++)    // Do front back order
  145.             {
  146.                 err = AEGetNthDesc(&closeDesc, index, typeWildCard, &theAEKeyword, &itemDesc);
  147.                 if (noErr != err) goto done;
  148.                 
  149.                 err = CloseDesc(&itemDesc, saveOpt);        // Call recursively
  150.                 if (noErr != err) goto done;
  151.  
  152.                 (void)AEDisposeDesc(&itemDesc);
  153.             }
  154.             break;
  155.             
  156.         default:    // Can coerce a document to a window
  157.             err = AECoerceDesc(&closeDesc, typeMyWndw, &windowDesc);
  158.             if (noErr != err) goto done;
  159.             err = CloseWindowDesc(&windowDesc, saveOpt);
  160.     }
  161.     
  162. done:
  163.     (void)AEDisposeDesc(&closeDesc);
  164.     (void)AEDisposeDesc(&windowDesc);
  165.     (void)AEDisposeDesc(&itemDesc);
  166.     
  167.     return(err);
  168. }
  169.  
  170.